package com.swing.sky.system.framework.security.utils;

import com.swing.sky.system.framework.security.service.UserDetailsImpl;
import com.swing.sky.system.module.domain.SysUserDO;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * \u4ECE\u8BA4\u8BC1\u4E2D\u83B7\u53D6\u7528\u6237\u4FE1\u606F
 */
public class UserDetailsUtil {
    /**
     * \u83B7\u53D6\u5F53\u524D\u8BA4\u8BC1\u7684\u7528\u6237\u4FE1\u606F
     *
     * @return \u7528\u6237\u4FE1\u606F
     */
    public static SysUserDO getUserDO() {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return userDetails.getUserDO();
    }

    /**
     * \u83B7\u53D6\u5F53\u524D\u8BA4\u8BC1\u7684\u7528\u6237\u540D
     *
     * @return \u7528\u6237\u540D
     */
    public static String getUsername() {
        return getUserDO().getUsername();
    }

    /**
     * \u83B7\u53D6\u5F53\u524D\u7528\u6237id
     * @return id
     */
    public static Long getUserId() {
        return getUserDO().getId();
    }
}